package com.kunkun.ui;

import com.kunkun.bean.employee;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class EmployeeManager extends JFrame {
    private JFrame frame; // 主窗口
    private JTable table; //表格模型
    private DefaultTableModel model;// 表格模型
    private JTextField nametextFieldSearch; // 搜索输入框
    private JButton btnSearch; //搜索按钮
    private JButton btnAdd; //添加按钮

    //准备一个静态集合装所有的员工的用户信息
    private static ArrayList<employee> employees = new ArrayList<>();
    static {
        employees.add(new employee(1, "员工1", 22, "男", "150", "LocalDate.now()", "职位1", 10000, "部门1"));
        employees.add(new employee(2, "员工2", 22, "男", "150", "LocalDate.now()", "职位2", 10000, "部门2"));
        employees.add(new employee(3, "员工3", 22, "男", "150", "LocalDate.now()", "职位3", 10000, "部门3"));
        employees.add(new employee(4, "员工4", 22, "男", "150", "LocalDate.now()", "职位4", 10000, "部门4"));
        employees.add(new employee(5, "员工5", 22, "男", "150", "LocalDate.now()", "职位5", 10000, "部门5"));
    }

    public EmployeeManager() {
    }
    //从登录页跳转到员工信息页时，把用户的用户名传递过来
    public EmployeeManager(String username) {
        frame = new JFrame("欢迎"+username+"，进入人事信息管理");
        frame.setVisible(true);// 显示主窗口
        initialize();
    }

    private void initialize() {
        frame.setBounds(300, 100, 800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());
        // 输入框和搜索按钮
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

        nametextFieldSearch = new JTextField(20);
        btnSearch = new JButton("搜索");
        btnAdd = new JButton("添加");

        topPanel.add(nametextFieldSearch);
        topPanel.add(btnSearch);
        topPanel.add(btnAdd);

        // 创建表格模型
        model = new DefaultTableModel(
                new Object[][]{}, // 初始数据
                new String[]{"ID", "姓名","年龄","性别","电话","入职日期","职位", "薪水","部门"} // 表头
        ) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false; // 设置所有单元格为不可编辑
            }
        };

        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);// 将表格添加到滚动面板中，提供滚动功能
        table.setRowHeight(30);

        // 从员工集合添加数据到表格
        for (employee emp : employees) {
            model.addRow(new Object[]{emp.getId(), emp.getName(), emp.getAge(), emp.getSex(), emp.getPhone(), emp.getDate(), emp.getPosition(), emp.getSalary(), emp.getDepartment()});
        }

        // 右键菜单
        JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem editItem = new JMenuItem("编辑");
        JMenuItem deleteItem = new JMenuItem("删除");
        popupMenu.add(editItem);
        popupMenu.add(deleteItem);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) { // 明确检查是否为鼠标右键
                    int row = table.rowAtPoint(e.getPoint());
                    if (row >= 0) {
                        table.setRowSelectionInterval(row, row);// 设置选中行
                        popupMenu.show(table, e.getX(), e.getY());// 显示右键菜单
                    }
                }
            }
        });

        // 绑定事件到菜单项
        editItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = table.getSelectedRow();
                if (selectedRow >= 0) {
                    int id = (Integer) model.getValueAt(selectedRow, 0); // 假设ID在第一列
                    JOptionPane.showMessageDialog(frame, "编辑 ID: " + id);
                    // 这里可以添加编辑逻辑，比如打开一个新的对话框让用户修改数据
                }
            }
        });

        deleteItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //拿到删除这一行数据的ID值
                //用ID去集合中删除这个对象，并把数据抹掉
                int selectedRow = table.getSelectedRow();
                if (selectedRow >= 0) {
                    int id = (Integer) model.getValueAt(selectedRow, 0); // 假设ID在第一列
                    // 从集合中删除对应的员工信息
                    for (employee emp : employees) {
                        if (emp.getId() == id) {
                            employees.remove(emp);
                            break;
                        }
                    }
                    // 从表格中删除该行
                    model.removeRow(selectedRow);
                }
            }
        });

        // 搜索按钮监听器
        btnSearch.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String searchValue = nametextFieldSearch.getText();
                boolean found = false; // 标志变量，记录是否找到匹配的员工

                // 通过输入员工姓名搜索该员工
                for (employee emp : employees) {
                    if (emp.getName().equals(searchValue)) {
                        // 找到匹配的员工，更新表格数据，使表格只显示该员工
                        model.setRowCount(0);
                        model.addRow(new Object[]{emp.getId(), emp.getName(), emp.getAge(), emp.getSex(), emp.getPhone(), emp.getDate(), emp.getPosition(), emp.getSalary(), emp.getDepartment()});
                        found = true; // 设置标志变量为 true
                        break; // 找到后退出循环
                    }
                }

                // 如果没有找到匹配的员工，显示弹窗
                if (!found) {
                    showAllEmployees();
                    JOptionPane.showMessageDialog(frame, "未找到该员工！");
                }
            }

            private void showAllEmployees() {
                //展示出所有员工
                model.setRowCount(0);
                for (employee emp : employees) {
                    model.addRow(new Object[]{emp.getId(), emp.getName(), emp.getAge(), emp.getSex(), emp.getPhone(), emp.getDate(), emp.getPosition(), emp.getSalary(), emp.getDepartment()});
                }
            }
        });


        // 添加按钮监听器
        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 这里可以添加添加新员工的逻辑
                new AddEmployee(EmployeeManager.this);
            }
        });

        frame.getContentPane().add(topPanel, BorderLayout.NORTH);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    }

    public void addEmployee(employee employee) {
        //添加到集合，展示到表格中
        employees.add(employee);
        // 添加到表格中
        model.addRow(new Object[]{employee.getId(), employee.getName(), employee.getAge(), employee.getSex(), employee.getPhone(), employee.getDate(), employee.getPosition(), employee.getSalary(), employee.getDepartment()});
    }
}